package com.penghaisoft.microwms.base.web.auth.controller;

import com.penghaisoft.microwms.base.web.auth.service.IMdProModelService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdProModel;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping(value = "/promodel")
@Slf4j
public class MdProModelController extends BaseController {

    @Autowired
    private IMdProModelService mdProModelService;

    /**
     * 新增记录
     *
     * @param mdProModel
     * @return
     */
    @PostMapping("c")
    public Resp create(MdProModel mdProModel) {

        Resp resp = new Resp();

        //验证型号是否重复 品名-定量-规格
        resp = mdProModelService.existence(mdProModel);
        if (Constant.FAIL.equals(resp.getCode())) {
            return resp;
        }

        mdProModel.preInsert();
        mdProModel.setActiveFlag("1");
        // 品名转大写
        String upCase = mdProModel.getProductName().toUpperCase();

        String density = mdProModel.getDensity();
        String spec = mdProModel.getSpec();
        // 拼接型号字符串
        StringBuilder stb = new StringBuilder(upCase);
        stb.append("-" + density);
        stb.append("-" + spec);
//			if (mdProModel.getMachineNumber() != null && mdProModel.getMachineNumber() != ""){
//				stb.append("(");
//				stb.append(mdProModel.getMachineNumber());
//				stb.append(")");
//			}
        // 型号、品名重新赋值
        mdProModel.setModel(stb.toString());
        mdProModel.setProductName(upCase);
        mdProModel.setCreateBy(getLoginName());
        mdProModel.setGmtCreate(new Date());
        resp = mdProModelService.create(mdProModel);
        resp.setMsg("添加成功");
        return resp;
    }

    /**
     * 删除记录（逻辑删除active_flag=0)
     *
     * @param mdProModel
     * @return
     */
    @PostMapping("d")
    public TransResult delete(MdProModel mdProModel) {
        mdProModel.preUpdate();
        Resp resp = mdProModelService.delete(mdProModel);
        return fromResp(resp);
    }

    /**
     * 查询列表
     *
     * @param
     * @return
     */
    @PostMapping("list")
    public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, MdProModel mdProModel) {
        Pager<MdProModel> result = mdProModelService.findListByCondition(page, rows, mdProModel);
        return success(result);
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @GetMapping("r/{id}")
    public TransResult queryById(@PathVariable String id) {
        MdProModel mdProModel = mdProModelService.findById(id);
        return success(mdProModel);
    }

    /**
     * 修改记录
     *
     * @param mdProModel
     * @return
     */
    @PostMapping("u")
    public TransResult update(MdProModel mdProModel) {
        mdProModel.preUpdate();
        Resp resp = mdProModelService.update(mdProModel);
        return fromResp(resp);
    }

    /**
     * @Title getModels
     * @Description 查询型号列表
     * @author wzhg
     * @date 2018年8月30日:上午10:11:11
     * @param page
     * @param rows
     * @param mdProModel
     * @return TransResult
     */
    @PostMapping("getModels/list")
    public TransResult getModels(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows,
            MdProModel mdProModel) {
        mdProModel.setActiveFlag("1");
        Pager<MdProModel> result = mdProModelService.findListByCondition(page, rows, mdProModel);
        return success(result);
    }

}
